package com.itheima.reggie.controller.backend;

import com.alibaba.fastjson.JSONObject;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/backends/statics")
@Slf4j
public class StaticsController {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 菜系营收占比分析
     * @return
     */
    @GetMapping("/dishCash")
    public Result<JSONObject> dishCash(){

        JSONObject res = new JSONObject();

        List<JSONObject> jsonObjects = dishService.selectVegetableAnalysis();
        res.put("dishCategoryCash",jsonObjects);
        return Result.success(res);
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     * @return
     */
    @GetMapping("/setmealCash")
    public Result<JSONObject> setmealCash(){
        JSONObject res = new JSONObject();
        List<JSONObject> jsonObjects = setmealService.selectEachAnalysis();
        res.put("setmealCash",jsonObjects);
        return Result.success(res);
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     * @return
     */
    @GetMapping("/cashPerMonth")
    public Result<JSONObject> cashPerMonth(){
        JSONObject res = new JSONObject();
        String[] monthList = {"2022-01","2022-02","2022-03","2022-04","2022-05","2022-06","2022-07","2022-08","2022-09","2022-10","2022-11","2022-12"};
        String[] dataList = {"0","0","0","0","0","0","0","0","0","0","0","0"};
        List<JSONObject> jsonObjects = orderMapper.selectEachAnalysis();

        if(jsonObjects != null && !jsonObjects.isEmpty()){
            for (JSONObject temp:jsonObjects){
                for (int i=0;i<monthList.length;i++){
                    if(monthList[i].equals(temp.getString("times"))){
                        dataList[i] = temp.getString("sums");
                        break;
                    }
                }
            }
        }
        res.put("dataList",dataList);
        res.put("monthList",monthList);
        return Result.success(res);
    }
}
